package net.cmkj.mine.core.admin.calculate;

import com.jfinal.aop.Inject;
import net.cmkj.mine.bean.Calculate;
import net.cmkj.mine.bean.Columns;
import net.cmkj.mine.common.anno.RequiresPermissions;
import net.cmkj.mine.common.controller.BaseController;

public class CalculateController extends BaseController {

    @Inject
    private CalculateService service;

    @RequiresPermissions(value={"/calculate"})
    public void index() throws Exception {
        setAttr("moduleid", getPara("moduleid"));
        render("calculateList.html");
    }

    @RequiresPermissions(value={"/calculate"})
    public void form() throws Exception {
        setAttr("moduleid", getParaToLong("moduleid"));
        render("calculateForm.html");
    }

    @RequiresPermissions(value={"/calculate"})
    public void edit() throws Exception {
        Long id = getParaToLong("id");
        if (id != null) {
            Calculate cal = service.findById(id);
            setAttr("item", cal);
            setAttr("column", Columns.dao.findById(cal.getColumnid()));
        }
        render("calculateEdit.html");
    }

    @RequiresPermissions(value={"/calculate"})
    public void getList() throws Exception {
        Long moduleid = getParaToLong("moduleid");
        renderJson(service.getList(getPage(), getRows(), moduleid));
    }

    @RequiresPermissions(value={"/calculate"})
    public void getCalList() throws Exception {
        Long moduleid = getParaToLong("moduleid");
        renderJson(service.getList(moduleid));
    }

    @RequiresPermissions(value={"/calculate"})
    public void save() throws Exception {
        renderJson(service.save(getModel(Calculate.class)));
    }

    @RequiresPermissions(value={"/calculate"})
    public void deleteByModuleid() throws Exception {
        Long moduleid = getParaToLong("moduleid");
        renderJson(service.deleteByModuleid(moduleid));
    }

    @RequiresPermissions(value={"/calculate"})
    public void delete() throws Exception {
        Long id = getParaToLong("id");
        renderJson(service.delete(id));
    }

    @RequiresPermissions(value={"/calculate"})
    public void getCalColumnList() throws Exception {
        Long moduleid = getParaToLong("moduleid");
        renderJson(service.getCalColumnList(moduleid));
    }

    @RequiresPermissions(value={"/calculate"})
    public void getCaledColumnList() throws Exception {
        Long moduleid = getParaToLong("moduleid");
        Long columnid = getParaToLong("columnid");
        renderJson(service.getCaledColumnList(moduleid, columnid));
    }

    @RequiresPermissions(value={"/calculate"})
    public void calculate() throws Exception {
        renderJson(service.calculate(getPara("formula"), getPara("mapstr")));
    }

}
